package org.example.ychdp.controller;

import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpSession;
import org.example.ychdp.entity.Comment;
import org.example.ychdp.entity.Concert;
import org.example.ychdp.entity.Order;
import org.example.ychdp.entity.User;
import org.example.ychdp.service.CommentService;
import org.example.ychdp.service.ConcertService;
import org.example.ychdp.service.OrderService;
import org.example.ychdp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/comment")
@Controller
public class adminCommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @Autowired
    private ConcertService concertService;

    @RequestMapping(value = "/commentManage")
    public ModelAndView testUserManage(HttpSession session,
                                       @RequestParam(required = false, defaultValue = "1") Integer page
//                                       @RequestParam(required = false, defaultValue = "2024-12") Date nickname
    ) {
        if (session.getAttribute("admin")==null){
            Map<String,Object> model = new HashMap<String,Object>();
            String errorMsg = "您未登录，请登录";
            model.put("errorMsg",errorMsg);
            ModelAndView modelAndView = new ModelAndView("admin/adminLogin",model);
            return modelAndView;
        }
        else {
            PageInfo<Comment> commentList = commentService.searchComment(page);
//            List<Comment> commentList = commentService.selectAllComment();
            Map<Integer, User> userMap = new HashMap<>();
            for (Comment comment : commentList.getList()) {
                Integer userId = comment.getUserid();  // 获取订单中的用户id
                if (!userMap.containsKey(userId) && userId!= null) {  // 如果还没查询过该用户，且userid不为空
                    User user = userService.selectByPrimaryKey(userId);  // 根据userid查询用户，这里假设findUserById是查询用户的方法
                    userMap.put(userId, user);
                }
            }

            Map<Integer, Concert> concertMap = new HashMap<>();
            for (Comment comment : commentList.getList()) {
                Integer concertId = comment.getConcertid();  // 获取订单中的演唱会id
                if (!concertMap.containsKey(concertId) && concertId!= null) {
                    Concert concert = concertService.selectByPrimaryKey(concertId);  // 根据concertid查询演唱会，这里假设selectByPrimaryKey是查询方法
                    concertMap.put(concertId, concert);
                }
            }
            Map<String,Object> model = new HashMap<String,Object>();
            model.put("commentL",commentList);
            model.put("activeMenuItem", "commentManage");
            model.put("userMap", userMap);  // 将用户信息的映射表也放入模型中，方便在视图层使用
            model.put("concertMap", concertMap);
            ModelAndView modelAndView = new ModelAndView("admin/Comment/commentManage",model);
            return modelAndView;
        }

    }

    @RequestMapping("/deleteComment")
    public String deleteUser(@RequestParam("id") int id)
    {
        int i = commentService.deleteCommentById(id);
        return "redirect:commentManage";
    }

}
